package com.rainbow.common.core.error;

import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.rainbow.common.core.toolkit.ErrorPrintHandle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;

/**
 * sentinel异常信息通用处理
 * @author 傅为地
 */
public class SentinelErrorUtil {

    private static Logger log = LoggerFactory.getLogger(SentinelErrorUtil.class);

    /**
     * 全局熔断处理
     * @param request 请求参数
     * @param body 内容
     * @param execution 参数内容
     * @param exception 异常内容
     * @return 返回结果
     */
    public static SentinelClientHttpResponse globalHandleFuse(HttpRequest request,
                                                     byte[] body,
                                                     ClientHttpRequestExecution execution,
                                                     BlockException exception) {
        log.error(ErrorPrintHandle.getErrorStackDetail(exception));
        return new SentinelClientHttpResponse("sentinel全局熔断异常:"+exception.getMessage());
    }

    /**
     * 全局降级处理
     * @param request 请求参数
     * @param body 内容
     * @param execution 参数内容
     * @param exception 异常内容
     * @return 返回结果
     */
    public static SentinelClientHttpResponse globalHandleFallback(HttpRequest request,
                                              byte[] body,
                                              ClientHttpRequestExecution execution,
                                              BlockException exception) {
        log.error(ErrorPrintHandle.getErrorStackDetail(exception));
        return new SentinelClientHttpResponse("sentinel全局降级异常:"+exception.getMessage());
    }


}
